import area from '../../utils/area.js';
Page({
  data: {
    winHeight: '100vh',
    toView: 'productBox',//锚点跳转的ID
    actionView: 'productBox',//控制导航显示
    productBoxTop: 0,//商品模块距离顶部的距离
    infoBoxTop: 0,//详情模块距离顶部的距离
    areaList:area,
    showAddess:false,//地区选择
    adressName:'河南郑州市',//
    specPopup:false,//规格弹层
    goods: {
      productType:'normal',//商品类别
      title: '美国伽力果（约680g/3个）',
      price: 2680,
      formatPrice: '',
      express: '免运费',
      remain: 19,
      thumb:
        'https://img.yzcdn.cn/public_files/2017/10/24/e5a5a02309a41f9f5def56684808d9ae.jpeg',
      specSkuList:[{id:0,proid:'2420',name:'石英粉1'},{id:0,proid:'2421',name:'石英粉2'},{id:0,proid:'2422',name:'石英粉3'}]
      
    },
    specdetail:"<table cellpadding=\"0\" cellspacing=\"1\" width=\"100%\" border=\"0\" class=\"Ptable\">\n                <tr><td class=\"tdTitle\">张宝华中国风格艺术丝巾</td><td>张宝华中国风格艺术丝巾</td></tr>\n        </table>",
    prodetail:'<p></p><p><img src=\"https://yceat-20200506.oss-cn-shanghai.aliyuncs.com/editor/210218/okrrBFOW4jfvGGG6Z0AKY0mzAoofhPz3EBTYkYF0.png\" style=\"max-width:100%;\"><img src=\"https://yceat-20200506.oss-cn-shanghai.aliyuncs.com/editor/210218/xqyqW7cV3ZDJaXDFTmhqt6lNnyWa9fc965JzXZWz.png\" style=\"max-width: 100%;\"><img src=\"https://yceat-20200506.oss-cn-shanghai.aliyuncs.com/editor/210218/va0j1Rmrx4s5brfhklT5wmF4fBpieLbuPjUzoOSY.png\" style=\"max-width: 100%;\"><img src=\"https://yceat-20200506.oss-cn-shanghai.aliyuncs.com/editor/210218/SKpU6vtJxAn9uXZ9M7YulbBOJUUX5to8yWgk2jZ5.png\" style=\"max-width: 100%;\"><img src=\"https://yceat-20200506.oss-cn-shanghai.aliyuncs.com/editor/210218/LX3bsss6FxInhQAyoHBEkVyex7Z4cg8TYLIQefr4.png\" style=\"max-width: 100%;\"><br></p>',
    buyType:false,//规格弹层默认显示立即购买
    buyloading:false,//立即购买状态加载
    specIndex:2420//默认规格显示
  },
  onLoad() {
    const { goods } = this.data;
    const formatPrice = `¥${(goods.price / 100).toFixed(2)}`;
    this.setData({
      goods: {
        ...goods,
        formatPrice,
      },
    });
  },

  toViewClick(e) {  //商品详情导航切换
    this.setData({
      toView: e.target.dataset.hash,  
      actionView: e.target.dataset.hash
    })
  
  },

  onShow(){
    // 获取各模块距离顶部的距离
    new Promise(resolve => {
      let query = wx.createSelectorQuery();
      query.select('#productBox').boundingClientRect();
      query.select('#infoBox').boundingClientRect();
      query.exec(function (res) {
        resolve(res);
      });
    }).then(res => {
      this.setData({
        productBoxTop: res[0].top,
        infoBoxTop: res[1].top
      });
    });
    // this.setData({//处理富文本格式
    //   specdetail:this.data.specdetail.replace(/<section/g, '<div').replace(/\/section>/g, '/div>')
    // })
  },
  scrollTo(e){//详情滚动切换
    let scrollTop = e.detail.scrollTop+42;  //滚动的Y轴
    if(scrollTop >= this.data.infoBoxTop){
      this.setData({
        actionView: 'infoBox'    
      })
    }else {
      this.setData({
        actionView: 'productBox'  
      })
    }
   
    //this.$apply();//WEPY更新数据
  },
  showAddressLay(){//选择地区
    console.log('00')
    this.setData({
      showAddess: true,
    });
  },
  confirmAddress({detail}){//确认地区
    this.setData({
      adressName:detail.values.map(item=>item.name).toString().replace(/,/g, "-"),
      firstAreaCode:detail.values[detail.values.length-1]['code'],
      showAddess:false
    })
  },
  onClose(){//关闭
    this.setData({
      specPopup:false,
      showAddess: false,
    });
  },
  onClickCart() {
    wx.navigateTo({
      url: '/pages/cart/index',
      success: () => {},
      error: () => {
        wx.showToast({
          icon: 'none',
          title: '打开购物车失败',
        });
      },
    });
  },

  onClickUser() {
    wx.navigateTo({
      url: '/pages/user/index',
      success: () => {},
      error: () => {
        wx.showToast({
          icon: 'none',
          title: '打开个人中心失败',
        });
      },
    });
  },

  onClickButton(e) {//立即购买加入购物车显示规格弹层
    this.setData({
      specPopup:true
    })
    if(e.target.dataset.buytype=='add'){
      this.setData({
        buyType:false
      })
    }else{
      this.setData({
        buyType:true
      })
    }
  },
  selectSpec(e){//选择规格
    var _this=this
    var selectId=e.target.dataset.proid
    this.setData({
      specIndex:selectId,
      buyloading:true
    })
 
    setTimeout(function(){
      _this.setData({
        buyloading:false
      })
    },100)
  },
  sorry() {
    wx.showToast({
      title: '暂无后续逻辑~',
      icon: 'none',
    });
  },
});